package com.example.snowli2.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import com.example.snowli2.MyDBHelper;

public class ContactContentProvider extends ContentProvider {
    private SQLiteDatabase db;//获取数据库对象
    private MyDBHelper dbHelper;//实现增删改查操作
    private UriMatcher uriMatcher;//声明uri的工具对象
    //声明authority常量
    private final String AUTHORITY = "com.contact.provider.jzh";
    //定义uri类型标识常量
    private final int URI_WORDS_ID = 1;
    private final int URI_WORD_ID = 2;
    public ContactContentProvider() {
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int n = 0;
        //判断uri合法性
        switch (uriMatcher.match(uri)){
            case URI_WORDS_ID:
                n = db.delete("contacts",selection,selectionArgs);
                break;
            case URI_WORD_ID:
                //获取id
                long id = ContentUris.parseId(uri);
                //判断id合法性
                if (id>0){
                    String whereClause = "id = "+id;
                    selection = whereClause;
                }
                //拼接where条件
                //删除
                n = db.delete("contacts",selection,selectionArgs);
                break;
            default:
                throw new UnsupportedOperationException("非法uri");
        }

        return n;
    }

    @Override
    public String getType(Uri uri) {
       return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        Uri resultUri = null;
        // 判断uri合法性
        switch (uriMatcher.match(uri)){
            case URI_WORDS_ID:
                //执行插入操作
                long rowId = db.insert("contacts", null, values);

                break;
            default:
                throw new UnsupportedOperationException("非法uri");
        }

        return resultUri;
    }

    @Override
    public boolean onCreate() {
        //1.获取数据库对象
        dbHelper = new MyDBHelper(
                this.getContext(),
                "Contact.db",
                null,
                1
        );
        db = dbHelper.getWritableDatabase();
        //获取uriMatcher对象
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        //2.注册uri
        uriMatcher.addURI(
                AUTHORITY,
                "contacts",
                URI_WORDS_ID  //uri标识
        );

        uriMatcher.addURI(
                AUTHORITY,
                "contact/#",
                URI_WORD_ID     //uri标识
        );

        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        Cursor cursor = null;
        System.out.println("match(uri)="+uriMatcher.match(uri));

        //判断uri的合法性
        switch (uriMatcher.match(uri)){
            case URI_WORDS_ID:
                //执行查询
                cursor = db.query("contacts",
                        projection,
                        selection,
                        selectionArgs,
                        null,
                        null,
                        sortOrder);
                break;
            case URI_WORD_ID:
                //获取uri末尾的id
                long id = ContentUris.parseId(uri);
                System.out.println("id为："+id);
                //判断是否大于0
                if (id>0){
                    String whereClause = "id=" + id;
                    //拼接最终的查询条件
                    selection = whereClause;
                }
                //执行查询
                cursor = db.query("contacts",
                        projection,
                        selection,
                        selectionArgs,
                        null,
                        null,
                        sortOrder);
                System.out.println("cursor="+cursor.getCount());
                System.out.println("查询id为"+id+"的数据成功");
                break;
            default:
                throw new UnsupportedOperationException("uri错误");
        }

        return cursor;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        int n = 0;
        //判断uri合法性
        switch (uriMatcher.match(uri)){
            case URI_WORDS_ID:
                n = db.update("contacts",
                        values,
                        selection,
                        selectionArgs);
                break;
            case URI_WORD_ID:
                //解析id
                long id = ContentUris.parseId(uri);
                //判断id是否合法
                if (id>0){
                    //将id解析成where条件
                    String whereClause = "id=" + id;
                    //拼接最终的where条件
                    selection = whereClause;

                }
                //更新
                n = db.update(
                        "contacts",
                        values,
                        selection,
                        selectionArgs
                );
                break;
            default:
                throw new UnsupportedOperationException("非法uri");
        }
        //更新
        return n;
    }
}